package com.bowen.demo.demo001.CyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * <h3>java-study</h3>
 * <p>循环栅栏多线程使用</p>
 * @author : zhang.bw
 * @date : 2021-04-28 11:40
 **/
public class CyclicBarrierDemo {

	public static void main(String[] args) {
		int N = 4;
		CyclicBarrier barrier  = new CyclicBarrier(N);
		for(int i=0;i<N;i++){
			new Writer(barrier).start();
		}
	}

	static class Writer extends Thread{

		private CyclicBarrier cyclicBarrier;

		public Writer(CyclicBarrier cyclicBarrier) {
			this.cyclicBarrier = cyclicBarrier;
		}

		@Override
		public void run() {
			try {
				//以睡眠来模拟线程需要预定写入数据操作
				Thread.sleep(5000);
				System.out.println(" 线程"+Thread.currentThread().getName()+" 写入数据完毕，等待其他线程写入完毕");
				cyclicBarrier.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}catch(BrokenBarrierException e){
				e.printStackTrace();
			}
			System.out.println("所有线程写入完毕，继续处理其他任务，比如数据操作");
		}
	}
}
